<template>
  <div id="record">
    <el-form :model="form">
      <el-form-item label="标题:">
        <el-input v-model="form.name" class="title"/>
      </el-form-item>
      <el-form-item label="内容:">
        <div style="border: 1px solid #ccc" class="tiny">
          <Toolbar
              style="border-bottom: 1px solid #ccc"
              :editor="editorRef"
              :defaultConfig="toolbarConfig"
              :mode="mode"
          />

          <Editor
              style="height: 500px; overflow-y: hidden"
              v-model="valueHtml"
              :defaultConfig="editorConfig"
              :mode="mode"
              @onCreated="handleCreated"
          />
        </div>
      </el-form-item>
      <el-button @click="CreateBlog" type="success">保存</el-button>
    </el-form>
  </div>
</template>
<script setup>
import "@wangeditor/editor/dist/css/style.css"
import { onBeforeUnmount, ref, reactive,shallowRef, onMounted } from "vue"
import { Editor, Toolbar } from "@wangeditor/editor-for-vue";
import {blog} from '@/view/dashboard/article/blog'

// 编辑器实例，必须用 shallowRef
const editorRef = shallowRef();

// 内容 HTML
const valueHtml = ref("");
const mode = ref("");
onMounted(() => {
  setTimeout(() => {
    valueHtml.value = "";
  }, 1500);
});

const toolbarConfig = {};

const editorConfig = { placeholder: "<h4>请输入内容...</h4>" };

// 组件销毁时，也及时销毁编辑器
onBeforeUnmount(() => {
  const editor = editorRef.value;
  if (editor == null) return;
  editor.destroy();
});

const AddCommand = () => {
  valueHtml.value = valueHtml.value + "" +
      "<input class='tiny'></input><style>input {background-color: cyan;width: 200px}</style>"
}

const handleCreated = (editor) => {
  editorRef.value = editor; // 记录 editor 实例，重要！
};

const form = reactive({
  name: "",
  content:valueHtml
})

const CreateBlog = async () => {
  console.log(form)
  await blog.Create(form)
}

const onSubmit = () =>{
  console.log(form.content)
}
</script>

<style scoped>
.tiny {
  margin: 5px 0;
  width: 100%;
}

.title {
  width: 200px;
}

:deep(.el-form-item__label) {
  font-size: 20px;
  font-family: 微软雅黑;
  font-weight: bold
}

</style>